home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Stuff / 3D_Reality / Shaders / Shader_Source / funkyfade.sl < prev    next >
Encoding:
Text File  |  1992-07-08  |  4.3 KB  |  143 lines

  1. surface 
  2. funkyfade(
  3.     float     frequency = 5,
  4.                 pbm       = 0.5,
  5.                 pw        = 0.5,
  6.         wRingscale= 10,
  7.         wKa        = 1.,
  8.         wKd        = 1.,
  9.         wKs        = 0.3,
  10.         wRoughness    = 0.1;
  11.  
  12. /* wood stuff */
  13.     color    wLightwood    = color (0.3, 0.12, 0.03),
  14.         wDarkwood    = color (0.05, 0.01, 0.005);
  15. /* blue marble stuff */
  16.     float   bmKd    = 1., 
  17.         bmKa    = 1.,
  18.         bmKs       = 0.3,
  19.         bmRoughness = .1,
  20.         bmTxtscale = 1;
  21.  
  22.       color bmSpecularcolor = color (0.0, 1.0, 1.0);
  23.  
  24. )
  25. {
  26.   float    smod = mod(s*frequency,1),
  27.     tmod = mod(t*frequency,1);
  28.  
  29.   point NN;
  30.   float y, z, r;
  31.   float sum = 0;
  32.   float i, freq = 7.0;
  33.   color bmCi, wCi;
  34.  
  35.   point PP;            /* scaled point in shader space */
  36.   float csp;           /* color spline parameter */
  37.   point Nf;            /* forward-facing normal */
  38.   point V;             /* for specular() */
  39.   float pixelsize, twice, scale, weight, turbulence;
  40.  
  41.   /* do blue_marble */
  42.   {
  43.     /* Obtain a forward-facing normal for lighting calculations. */
  44.     Nf = faceforward( normalize(N), I);
  45.     V = normalize(-I);
  46.     
  47.     /*
  48.      * Compute "turbulence" a la [PERLIN85]. Turbulence is a sum of 
  49.      * "noise" components with a "fractal" 1/f power spectrum. It gives the
  50.      * visual impression of turbulent fluid flow (for example, as in the 
  51.      * formation of blue_marble from molten color splines!). Use the 
  52.      * surface element area in texture space to control the number of 
  53.      * noise components so that the frequency content is appropriate 
  54.      * to the scale. This prevents aliasing of the texture.
  55.      */
  56.     PP = transform("shader", P) * bmTxtscale;
  57.     pixelsize = sqrt(area(PP));
  58.     twice = 2 * pixelsize;
  59.     turbulence = 0;
  60.     for (scale = 1; scale > twice; scale /= 2) 
  61.       turbulence += scale * noise(PP/scale);
  62.     
  63.     /* Gradual fade out of highest-frequency component near limit */
  64.     if (scale > pixelsize) {
  65.       weight = (scale / pixelsize) - 1;
  66.       weight = clamp(weight, 0, 1);
  67.       turbulence += weight * scale * noise(PP/scale);
  68.     }
  69.     
  70.     /*
  71.      * Magnify the upper part of the turbulence range 0.75:1
  72.      * to fill the range 0:1 and use it as the parameter of
  73.      * a color spline through various shades of blue.
  74.      */
  75.     csp = clamp(4 * turbulence - 3, 0, 1);
  76.     bmCi = color spline(csp,
  77.              color (0.25, 0.25, 0.35),  /* pale blue        */
  78.              color (0.25, 0.25, 0.35),  /* pale blue        */
  79.              color (0.20, 0.20, 0.30),  /* medium blue      */
  80.              color (0.20, 0.20, 0.30),  /* medium blue      */
  81.              color (0.20, 0.20, 0.30),  /* medium blue      */
  82.              color (0.25, 0.25, 0.35),  /* pale blue        */
  83.              color (0.25, 0.25, 0.35),  /* pale blue        */
  84.              color (0.15, 0.15, 0.26),  /* medium dark blue */
  85.              color (0.15, 0.15, 0.26),  /* medium dark blue */
  86.              color (0.10, 0.10, 0.20),  /* dark blue        */
  87.              color (0.10, 0.10, 0.20),  /* dark blue        */
  88.              color (0.25, 0.25, 0.35),  /* pale blue        */
  89.              color (0.10, 0.10, 0.20)   /* dark blue        */
  90.              );
  91.     
  92.     /* Multiply this color by the diffusely reflected light. */
  93.     bmCi *= bmKa*ambient() + bmKd*diffuse(Nf);
  94.     
  95.     /* Adjust for opacity. */
  96.     Oi = Os;
  97.     bmCi = bmCi * Oi;
  98.     
  99.     /* Add in specular highlights. */
  100.     bmCi += bmSpecularcolor * bmKs * specular(Nf,V,bmRoughness);
  101.   }
  102.   
  103.   /* do wood */
  104.   {
  105.     /*
  106.      * Compute the forward-facing normal NN and the vector
  107.      * toward the ray origin V, both normalized.
  108.      * These vectors are used by "specular" and "diffuse". */
  109.     NN = faceforward(normalize(N),I);
  110.     V = -normalize(I);
  111.     
  112.     /* put point in shader space and perturb it to add irregularity */
  113.     PP = transform("shader", P);
  114.     PP += noise(PP);
  115.     
  116.     /* compute radial distance r from PP to axis of "tree" */
  117.     y = ycomp(PP);
  118.     z = zcomp(PP);
  119.     r = sqrt(y*y + z*z);
  120.     
  121.     /* map radial distance r into ring position [0,1] */
  122.     r *= wRingscale;
  123.     r += abs(noise(r));
  124.     r -= floor(r);            /* == mod(r,1) */
  125.     
  126.     /* use ring position r to select wood color */
  127.     r = smoothstep(0, 0.8, r) - smoothstep(0.83, 1.0, r);
  128.     wCi = mix(wLightwood, wDarkwood, r);
  129.     
  130.     /* shade using r to vary shininess */
  131.     Oi = Os;
  132.     wCi = Oi * wCi * (wKa * ambient() + wKd * diffuse(NN))
  133.       + (0.3*r + 0.7) * wKs * specular(NN, V, wRoughness);
  134.   }
  135.  
  136.   /* combine the two */
  137.   if(smod<0.5)
  138.     Ci=(wCi*smod*2) + (bmCi*(1.0-(smod*2)));
  139.   else Ci=(wCi*(2.0-smod*2)) + (bmCi*(smod*2 -1));
  140. }
  141.  
  142.   
  143.